package org.max.cjs.util;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
//import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//@WebFilter(filterName = "AuthFilter", urlPatterns = {"*.jsf", "*.xhtml"})
public class AuthFilter implements Filter {

    public AuthFilter() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        try {

            // check whether session variable is set
            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse res = (HttpServletResponse) response;
            HttpSession ses = req.getSession(false);
            // allow user to proccede if url is login.xhtml or user logged in or
            // user is accessing any page in 
            // public folder
            String reqURI = req.getRequestURI();
            if (reqURI.indexOf("/index.jsf") >= 0
                    || (ses != null && ses.getAttribute(FacesUtil.ATTRIBUTE_USER) != null)
                    || reqURI.indexOf("/public/") >= 0
                    || reqURI.contains("javax.faces.resource")) {
                
                chain.doFilter(request, response);
                
            } else {
                //res.sendRedirect(req.getContextPath() + "/pageNotFound.jsf");
                res.sendRedirect(req.getContextPath() + "/index.jsf");
                
            } 
        } catch (Throwable t) {
            t.printStackTrace();
            System.out.println("Causado por: " + t.getCause());
            System.err.println("Mensaje de Err or en Filtro: " + t.getMessage());
        }
    } // doFilter

    @Override
    public void destroy() {
    }
}
